package com.bjgov.scoresys.service;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.ObjectNotFoundException;

import com.bjgov.scoresys.dao.UserProjectDAO;
import com.bjgov.scoresys.entity.UserProject;
import com.bjgov.scoresys.util.Page;
import com.bjgov.scoresys.util.PageUtil;
import com.bjgov.scoresys.util.Result;

public class UserProjectHandler {
	protected static UserProjectDAO dao = new UserProjectDAO();
	
	public static int save(UserProject entity) {
		return dao.save(entity);
	}
	
	public static void delete(UserProject entity) {
		dao.delete(entity);
	}
	
	public static List<UserProject> getCompleteProjectList(int userId) {
		return dao.findByUserIdProjectStatus(userId);
	}
	
	/**
	 * 正常进行评分的项目
	 * @param userId
	 * @param page
	 * @return
	 * @throws HibernateException
	 * @throws ObjectNotFoundException
	 */
	public static Result listByUser(int userId, Page page) throws HibernateException,
			ObjectNotFoundException {
		int totalRecords = dao.countByUserNowSocre(userId);
		page = PageUtil.createPage(page, totalRecords);
		List users = dao.getByUserPage(userId, page);
		return new Result(users, page);
	}
	
	/**
	 * 已完成的评分项目
	 * @param userId
	 * @param page
	 * @return
	 * @throws HibernateException
	 * @throws ObjectNotFoundException
	 */
	public static Result listByUserComplete(int userId, Page page) throws HibernateException,
	ObjectNotFoundException {
		int totalRecords = dao.countByUserScoreComplete(userId);
		page = PageUtil.createPage(page, totalRecords);
		List users = dao.getByUserCompletePage(userId, page);
		return new Result(users, page);
	}
	
	public static Result list(Page page) throws HibernateException,
			ObjectNotFoundException {
		int totalRecords = dao.count();
		page = PageUtil.createPage(page, totalRecords);
		List users = dao.getByPage(page);
		return new Result(users, page);
	}	
	
	public static UserProject getUserProjectByUidPid(int userId, int projectId) {
		return dao.findByUserIdProjectId(userId, projectId);
	}
}
